package com.framework.bdf4j.comm.mail;

import java.util.Map;

import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import freemarker.template.Template;
import lombok.extern.slf4j.Slf4j;

/**
 * <b>文件名： </b>MailHelper.java<br/>
 * <b>类描述： </b>邮件帮助类<br/>
 */
@Slf4j
public class MailHelper {

	@Autowired
	private JavaMailSender mailSender;
	private FreeMarkerConfigurer freeMarkConfiger;
	private String mailFrom;
	private String mailTemplate;

	public boolean sendMail( String to, String subject, Map<String, Object> data ) {
		return sendMail(this.mailFrom, new String[] {to}, subject, data);
	}
	
	public boolean sendMail( String[] to, String subject, Map<String, Object> data ) {
		return sendMail(this.mailFrom, to, subject, data);
	}

	public boolean sendMail(String from, String to, String subject, Map<String, Object> data ) {
		return sendMail(from, new String[] {to}, subject, data);
	}

	/**
	 * 发送邮件
	 * 
	 * @param from 发送者（邮件地址）
	 * @param to 发送至（邮件地址）
	 * @param subject 主题（内容标题）
	 * @param tplName 模板名称（xxx.vm ， 模板地址：/WEB-INF/views/mail/..）
	 * @param data 参数（模板参数）
	 * @return
	 */
	public boolean sendMail(String from, String[] to, String subject, Map<String, Object> data )
	{
		try {
			MimeMessage msg = mailSender.createMimeMessage();
			MimeMessageHelper msgHelper = new MimeMessageHelper(msg, "utf-8");
			msgHelper.setFrom(from);
			msgHelper.setTo(to);
			msgHelper.setSubject(subject);
			msgHelper.setText(this.getHtmltext(this.mailTemplate, data), true);
			mailSender.send(msg);
			return true;
		} catch ( Exception e ) {
			log.error("发送邮件失败.", e);
		}
		return false;
	}
	
    public boolean sendMail(String to, String subject, String data) throws Exception {
        boolean result = false;
        MimeMessage msg = mailSender.createMimeMessage();
        MimeMessageHelper msgHelper = new MimeMessageHelper(msg, "utf-8");
        msgHelper.setFrom(this.mailFrom);
        msgHelper.setTo(to);
        msgHelper.setSubject(subject);
        msgHelper.setText(data, true);
        mailSender.send(msg);
        result = true;
        return result;
    }

	/**
	 * <b>方法描述：</b> velocity 模板转 html <br/>
	 * <b>创建者：</b> 于建军 <br/>
	 * <b>创建时间：</b> 2017年6月19日 下午2:45:55 <br/>
	 *
	 * @param tplName 模板文件名称
	 * @param data 参数
	 */
	private String getHtmltext( String tplName, Map<String, Object> data ) {
		String htmlText = "";
		try {
			Template template = freeMarkConfiger.getConfiguration().getTemplate(tplName);
			htmlText = FreeMarkerTemplateUtils.processTemplateIntoString(template, data);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	    return htmlText;
	}

	public void setMailFrom( String mailFrom ) {
		this.mailFrom = mailFrom;
	}

	public void setMailTemplate(String mailTemplate) {
		this.mailTemplate = mailTemplate;
	}

	public void setFreeMarkConfiger(FreeMarkerConfigurer freeMarkConfiger) {
		this.freeMarkConfiger = freeMarkConfiger;
	}
	
}
